﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class UIFieldReadState : MonoBehaviour
    {
        public UIFieldRunMap page;
        public UISlider slider;
        public float usedTime;
        public float fullTime;
        public UILabel cost;
        public int intCost;
        public GameObject fish ,mine;
        public string id;
        OkHander ok;
        public void Init(string id,OkHander ok)
        {
            this.id = id;
            this.ok = ok;
            var res = Dict.Blo.DictFieldResourceBlo.GetOne(id);
            //var temp = res.cost.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
            //intCost = int.Parse(temp[1]);

            var dictCost = Dict.Blo.DictFieldResourceBlo.GetCost(id);
            intCost = dictCost["tiredness"];

            if (StaticData.tirednessCost > 0)
                intCost = StaticData.tirednessCost;

            this.usedTime = res.gatherTime * 0.001f; 
            this.fullTime = usedTime==0?1f:usedTime;

            bool fish = res.id.StartsWith("group_fish_");

            Set(fish, intCost);

            Show();
        }

        void Set(bool fish,int cost)
        {
            this.fish.SetActive(fish);
            this.mine.SetActive(!fish);
            this.cost.text = string.Format(TableTextBlo.GetText("field_get_res_cost"), cost);
        }
        
        void Update()
        {
            usedTime -= Time.deltaTime;
            
            slider.value = Mathf.Clamp01( usedTime / fullTime );
            if(slider.value==0)
            {
                Complete();
            }
        }

        void Show()
        {
            gameObject.SetActive(true);
            EasyTouchTool.Instance.Hide();
        }

        void Hide()
        {
            gameObject.SetActive(false);
            EasyTouchTool.Instance.ShowJoystick();
            page.Refresh();
        }

        void Complete()
        {
            ok();
            
            Hide();
        }
    }
}